package com.zjm.principle.dip;

/**
 * 依赖倒置原则
 *
 */
public class DependencyInversion {
    public static void main(String[] args) {
        MessageService emailService = new EmailService();
        MessageService smsService = new SMSService();

        NotificationService notification1 = new NotificationService(emailService);
        NotificationService notification2 = new NotificationService(smsService);

        notification1.sendNotification("你好，这是一封邮件！");
        notification2.sendNotification("你好，这是一条短信！");
    }
}


// 抽象：定义统一的接口
interface MessageService {
    void sendMessage(String message);
}

// 细节：实现 Email 消息服务
class EmailService implements MessageService {
    public void sendMessage(String message) {
        System.out.println("发送邮件: " + message);
    }
}

// 细节：实现 SMS 消息服务
class SMSService implements MessageService {
    public void sendMessage(String message) {
        System.out.println("发送短信: " + message);
    }
}

// 高层模块：不依赖具体实现，只依赖抽象
class NotificationService {
    private MessageService messageService;

    // 构造函数注入依赖
    public NotificationService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void sendNotification(String message) {
        messageService.sendMessage(message);
    }
}
